package com.hspedu.Object;

import java.util.Objects;

/**
 * 重写equals方法
 * 判断两个Person对象的内容是否相等，
 * 如果两个Person对象的各个属性值都一样，则返回true，反之false
 */
public class EqualsExercise01 {
    public static void main(String[] args) {
        Person1 p1 = new Person1("张三", 20, 'm');
        Person1 p2 = new Person1("张三", 20, 'm');
        Person1 p3 = new Person1("李四", 21, 'w');
        System.out.println(p1.equals(p2));
        System.out.println(p1.equals(p3));

    }
}

class Person1 {
    private String name;
    private int age;
    private char gender;

    //equals方法


    @Override
    public boolean equals(Object o) {
        //如果比较的两个对象是同一个对象，则直接返回true
        if (this == o) return true;
        //类型判断
        if (o instanceof Person1) {
            //向下转型
            Person1 p = (Person1) o;
            return this.name.equals(p.name) && this.age == p.age && this.gender == p.gender;
        } else {
            return false;
        }
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age, gender);
    }

    public Person1(String name, int age, char gender) {
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    public Person1() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public char getGender() {
        return gender;
    }

    public void setGender(char gender) {
        this.gender = gender;
    }
}